﻿Imports PTIC_Util
Imports OMANE.Common.UI
Imports PTIC_GestionBDD
Imports System.Collections.ObjectModel
Imports System.Windows.Input
Imports System.Windows
Imports Microsoft.Practices.Prism.Commands

Namespace ViewModels
    Public Class ViewModelCorrespondance
        Inherits V2ViewModelTabSection

        Private _tableBDD As tableBDD

#Region "Constructeur"
        Public Sub New(ByVal CorrespondanceConsultee As PTIC_GestionBDD.Correspondance)
            _CorrespondanceConsultee = CorrespondanceConsultee.copy
            _tableBDD = CorrespondanceConsultee.traitement
            If Not _CorrespondanceConsultee.chBDD Is Nothing Then _chEntreeSave = _CorrespondanceConsultee.chBDD.copy Else _chEntreeSave = Nothing
            _typeChEntreeSave = _CorrespondanceConsultee.typeChEntree
            If _CorrespondanceConsultee.typeChEntree = DefinitionsTypes.typeChEntreeMapping.CONSTANT Then
                _chEntreeSave = Nothing
                _typeChEntreeSave = DefinitionsTypes.typeChEntreeMapping.NON_INITIALISE
            End If
            If _CorrespondanceConsultee.typeChEntree = DefinitionsTypes.typeChEntreeMapping.TABLE Then
                _CorrespondanceConsultee.chOutil.valeursDistinctes = _tableBDD.ListeChampsEntree.Item(_CorrespondanceConsultee.chBDD.Nom).chOutil.valeursDistinctes()
            End If
        End Sub
#End Region

#Region "Correspondance consultée"
        Private _CorrespondanceConsultee As Correspondance
        Public ReadOnly Property CorrespondanceConsultee As Correspondance
            Get
                Return _CorrespondanceConsultee
            End Get
        End Property
#End Region

#Region "Possibilité de segmenter la variable"
        Private _VariableSegItemsSource As ObservableCollection(Of String)
        Public ReadOnly Property VariableSegItemsSource As ObservableCollection(Of String)
            Get
                If _VariableSegItemsSource Is Nothing Then
                    _VariableSegItemsSource = New ObservableCollection(Of String)
                    _VariableSegItemsSource.Add("VRAI")
                    _VariableSegItemsSource.Add("FAUX")
                End If
                Return _VariableSegItemsSource
            End Get
        End Property
#End Region

#Region "Gestion des valeurs constantes"
        Private _chEntreeSave As ChampsBDD
        Private _typeChEntreeSave As TypeChEntreeMapping

        Private _ValeurFixeChampImporte As String
        Public Property ValeurFixeChampImporte As String
            Get
                Dim result As String = ""
                If _CorrespondanceConsultee.typeChEntree = DefinitionsTypes.typeChEntreeMapping.CONSTANT Then
                    If _CorrespondanceConsultee.chOutil.Format.typeStock.Name = "String" Then
                        result = "=""" & _CorrespondanceConsultee.chBDD.Nom & """"
                    Else
                        result = "=" & _CorrespondanceConsultee.chBDD.Nom
                    End If
                Else
                    If ValeurFixeChampsImporte_HasFocus Then
                        If _CorrespondanceConsultee.chOutil.Format.typeStock.Name = "String" Then
                            result = "="""""
                        Else
                            result = "="
                        End If
                    Else
                        result = ""
                    End If
                End If
                Return result
            End Get

            Set(ByVal value As String)

                OnPropertyChanging("ValeurFixeChampImporte")
                Dim ChangementIsValide As Boolean = True

                'Validation du masque de saisie de la TextBox
                Dim isNewMasqueValide As Boolean = True
                If value = "" Then
                    isNewMasqueValide = True
                ElseIf _CorrespondanceConsultee.chOutil.Format.typeStock.Name = "String" Then
                    If (Microsoft.VisualBasic.Left(value, 2) <> "=""" Or _
                        Microsoft.VisualBasic.Right(value, 1) <> """" Or _
                        Len(value) < 3) Then
                        isNewMasqueValide = False
                    End If
                ElseIf _CorrespondanceConsultee.chOutil.Format.typeStock.Name = "Double" Then
                    If (Microsoft.VisualBasic.Left(value, 1) <> "=") Then
                        isNewMasqueValide = False
                    End If
                End If

                'Validation de la nouvelle valeur (pas d'espace pour les chaînes de caractère, IsNumeric pour les double)
                'Détermination du nouveau type de champs en entrée
                'Détermination du nouveau masque
                If isNewMasqueValide Then

                    'Récupérer la vrai valeur du paramètre
                    Dim newValeur As String = ""
                    If value = "" Then
                        If Not _chEntreeSave Is Nothing Then
                            _CorrespondanceConsultee.chBDD.Nom = _chEntreeSave.Nom
                            _CorrespondanceConsultee.typeChEntree = _typeChEntreeSave
                        Else
                            _CorrespondanceConsultee.setChEntree(Nothing)
                        End If

                    ElseIf _CorrespondanceConsultee.chOutil.Format.typeStock.Name = "String" Then
                        newValeur = Microsoft.VisualBasic.Right(value, Len(value) - 2)
                        newValeur = Microsoft.VisualBasic.Left(newValeur, Len(newValeur) - 1)
                        newValeur = newValeur.Replace(" ", "_")
                        If newValeur <> "" Then
                            If Not _CorrespondanceConsultee.chBDD Is Nothing Then
                                _CorrespondanceConsultee.chBDD.Nom = newValeur
                                _CorrespondanceConsultee.typeChEntree = DefinitionsTypes.typeChEntreeMapping.CONSTANT
                            Else
                                _CorrespondanceConsultee.setChEntree(New ChampsBDD(newValeur, _CorrespondanceConsultee.chOutil.Format.copy))
                                _CorrespondanceConsultee.typeChEntree = DefinitionsTypes.typeChEntreeMapping.CONSTANT
                            End If
                        Else
                            If Not _chEntreeSave Is Nothing Then
                                _CorrespondanceConsultee.chBDD.Nom = _chEntreeSave.Nom
                                _CorrespondanceConsultee.typeChEntree = _typeChEntreeSave
                            Else
                                _CorrespondanceConsultee.setChEntree(Nothing)
                            End If
                        End If

                    ElseIf _CorrespondanceConsultee.chOutil.Format.typeStock.Name = "Double" Then
                        newValeur = Microsoft.VisualBasic.Right(value, Len(value) - 1)
                        If Not IsNumeric(newValeur) And newValeur <> "" Then ChangementIsValide = False

                        If newValeur <> "" And ChangementIsValide Then
                            If Not _CorrespondanceConsultee.chBDD Is Nothing Then
                                _CorrespondanceConsultee.chBDD.Nom = newValeur
                                _CorrespondanceConsultee.typeChEntree = DefinitionsTypes.typeChEntreeMapping.CONSTANT
                            Else
                                _CorrespondanceConsultee.setChEntree(New ChampsBDD(newValeur, _CorrespondanceConsultee.chOutil.Format.copy))
                                _CorrespondanceConsultee.typeChEntree = DefinitionsTypes.typeChEntreeMapping.CONSTANT
                            End If

                        ElseIf ChangementIsValide Then
                            If Not _chEntreeSave Is Nothing Then
                                _CorrespondanceConsultee.chBDD.Nom = _chEntreeSave.Nom
                                _CorrespondanceConsultee.typeChEntree = _typeChEntreeSave
                            Else
                                _CorrespondanceConsultee.setChEntree(Nothing)
                            End If
                        End If
                    End If

                Else
                    ChangementIsValide = False
                End If

                OnPropertyChanged("IsValeursSourceVisible")
                OnPropertyChanged("ValeurFixeChampImporte")

            End Set
        End Property

        Public Property ValeurFixeChampsImporte_HasFocus As Boolean
            Get
                Return _ValeurFixeChampsImporte_HasFocus
            End Get
            Set(ByVal value As Boolean)
                OnPropertyChanging("ValeurFixeChampsImporte_HasFocus")
                Dim e As New PropertyChangeEventArgs(Of Boolean)(ValeurFixeChampsImporte_HasFocus, value)
                If e.IsValide Then RaiseEvent ValeurFixeChampsImporte_HasFocusChanging(Me, e)
                If e.IsValide Then
                    _ValeurFixeChampsImporte_HasFocus = e.NewValue
                    OnPropertyChanged("ValeurFixeChampsImporte_HasFocus")
                    RaiseEvent ValeurFixeChampsImporte_HasFocusChanged(Me, e)
                End If
            End Set
        End Property
        Private _ValeurFixeChampsImporte_HasFocus As Boolean = False
        Public Event ValeurFixeChampsImporte_HasFocusChanging As PropertyChangeEventHandler(Of Boolean)
        Public Event ValeurFixeChampsImporte_HasFocusChanged As PropertyChangeEventHandler(Of Boolean)
        Public Sub onValeurFixeChampsImporte_GotFocus()
            ValeurFixeChampsImporte_HasFocus = True
            OnPropertyChanged("ValeurFixeChampImporte")
        End Sub
        Public Sub onValeurFixeChampsImporte_LostFocus()
            ValeurFixeChampsImporte_HasFocus = False
            OnPropertyChanged("ValeurFixeChampImporte")
        End Sub

        Public ReadOnly Property IsTxtValFixeEnabled As Boolean
            Get
                If _CorrespondanceConsultee.chOutil.Format.typeStock.Name = "String" Then Return True Else Return False
            End Get
        End Property
#End Region

#Region "Rafraichissement / Affichage des valeurs source des valeurs source"
        Private _cmdRefreshValuesSource As New DelegateCommand(Of Correspondance)(AddressOf refreshValuesSource)
        Public ReadOnly Property CmdRefreshValuesSource As ICommand
            Get
                Return _cmdRefreshValuesSource
            End Get
        End Property
        Private Sub refreshValuesSource(ByVal CorrespEditee As Correspondance)
            'Dans le traitement en entrée, calcule les valeurs distinctes de la correspondance en entrée
            If CorrespEditee.typeChEntree = DefinitionsTypes.typeChEntreeMapping.TABLE Then
                If CorrespEditee.chOutil.valeursDistinctes Is Nothing Then
                    _tableBDD.CalculerValeursEntree(CorrespEditee.chBDD.Nom)
                    CorrespEditee.chOutil.valeursDistinctes = _tableBDD.ListeChampsEntree.Item(CorrespEditee.chBDD.Nom).chOutil.valeursDistinctes()
                End If
            End If
        End Sub

        Public ReadOnly Property IsValeursSourceVisible As Visibility
            Get
                If _CorrespondanceConsultee.typeChEntree = DefinitionsTypes.typeChEntreeMapping.TABLE Then Return Visibility.Visible Else Return Visibility.Hidden
            End Get
        End Property


#End Region

#Region "OK"
        Private _OKCommand As New DelegateCommand(Of Object)(AddressOf OKExecute, AddressOf OKCanExecute)
        Public ReadOnly Property OKCommand As DelegateCommand(Of Object)
            Get
                Return _OKCommand
            End Get
        End Property
        Private Sub OKExecute(ByVal parameter As Object)
            'Validation de la correspondance. Quels sont les éléments qui peuvent changer :
            '   - Le nom du champs source
            '   - La nature de la correspondance
            '   - Le fait, ou non, de pouvoir inclure la variable dans la segmentation
            Dim corresp As Correspondance = _tableBDD.ListeChampsSortie(_CorrespondanceConsultee.description)
            corresp.setChEntree(_CorrespondanceConsultee.chBDD)
            If Not _CorrespondanceConsultee.informat Is Nothing Then
                corresp.informat.nom = _CorrespondanceConsultee.informat.nom
            End If
            corresp.typeChEntree = _CorrespondanceConsultee.typeChEntree
            corresp.possSeg = _CorrespondanceConsultee.possSeg
        End Sub
        Private Function OKCanExecute(ByVal parameter As Object) As Boolean
            Return True
        End Function
#End Region

#Region "Annuler"
        Private _AnnulerCommand As New DelegateCommand(Of Object)(AddressOf AnnulerExecute, AddressOf AnnulerCanExecute)
        Public ReadOnly Property AnnulerCommand As DelegateCommand(Of Object)
            Get
                Return _AnnulerCommand
            End Get
        End Property
        Private Sub AnnulerExecute(ByVal parameter As Object)

        End Sub
        Private Function AnnulerCanExecute(ByVal parameter As Object) As Boolean
            Return True
        End Function
#End Region

    End Class
End Namespace